const {v4: uuidv4} = require('uuid')
const Projects = require('../../models/projects')

const getProjectsHandler = async (_, res) => {
  try {
    const projectsList = await Projects.find()
    projectsList.length
      ? res.status(200).json(projectsList)
      : res.status(404).json({success: false, msg: 'Документы не найдены'})
  } catch (e) {
    res.status(500).json({success: false, msg: e})
  }
}

const postNewProjectHandler = async (req, res) => {
  const {title, status} = req.body
  try {
    const result = await Projects.create({
      id: uuidv4(),
      title,
      status,
    })
    res.status(200).json(result)
  } catch (e) {
    res.status(500).json({success: false, msg: e})
  }
}

module.exports = {getProjectsHandler, postNewProjectHandler}
